/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import java.awt.Toolkit;
import java.io.File;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

/**
 *
 * @author KIENLT
 */
public class tree extends javax.swing.JFrame {

    /**
     * Creates new form tree
     */
    public tree() {
        initComponents();
        buildtree();
        this.setLocationRelativeTo(null);
        try {
            setIconImage(Toolkit.getDefaultToolkit().getImage(new File("src\\Images\\Icon-Form.png").getCanonicalPath()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    JScrollPane jScrollPane1;
    JTree tree1;

    private void buildtree() {
        jScrollPane1 = new javax.swing.JScrollPane();
        tree1 = createJTree();

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new treeobject("1111", "root"));

        root.add(new DefaultMutableTreeNode(new treeobject("11", "12")));
        root.add(new DefaultMutableTreeNode(new treeobject("12", "23")));
        root.add(new DefaultMutableTreeNode(new treeobject("132", "45")));

        DefaultMutableTreeNode root1 = new DefaultMutableTreeNode(new treeobject("1111", "xxx"));
        root.add(root1);
        root1.add(new DefaultMutableTreeNode(new treeobject("11", "xx1")));
        root1.add(new DefaultMutableTreeNode(new treeobject("12", "xx2")));
        root1.add(new DefaultMutableTreeNode(new treeobject("132", "xx3")));

        DefaultTreeModel treemodel = new DefaultTreeModel(root);
        treemodel.setRoot(root);

        jTree1.setModel(treemodel);
//
        jTree1.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                System.out.println("You selected : " + node);
                if (node == null) {
                    return;
                }
                System.out.println("aa : "+node.getRoot());
                
                Object info = node.getUserObject();
                treeobject t = (treeobject) info;
                if (node.isRoot()) {
                    System.out.println("x : " + t.getId());
                }
                if (node.isLeaf()) {
                    System.out.println("xx : " + t.getId());
                }
            }
        });

//        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//
//        jScrollPane1.setViewportView(tree1);
//
//        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
//        getContentPane().setLayout(layout);
//        layout.setHorizontalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(layout.createSequentialGroup()
//                .addContainerGap()
//                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
//                .addContainerGap(235, Short.MAX_VALUE))
//        );
//        layout.setVerticalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(layout.createSequentialGroup()
//                .addContainerGap()
//                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
//                .addContainerGap(101, Short.MAX_VALUE))
//        );
//
//        this.setLayout(layout);
//        
//        pack();
    }

    private JTree createJTree() {

        /*
         * Create JTree
         */

        JTree jTree = new JTree();
        jTree.setRootVisible(false);
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root node, should be invisible");
        DefaultTreeModel defaultTreeModel = new DefaultTreeModel(rootNode);

        jTree.setModel(defaultTreeModel);

        /*
         * Add nodes to it
         */

        DefaultMutableTreeNode parentNode;
        DefaultMutableTreeNode node;


        parentNode = (DefaultMutableTreeNode) defaultTreeModel.getRoot();
        node = new DefaultMutableTreeNode(new treeobject("1", "node1"));
        addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);
        
        parentNode = node;
        node = new DefaultMutableTreeNode(new treeobject("5", "node5"));
        addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);
        
        parentNode = node;
        node = new DefaultMutableTreeNode(new treeobject("2", "node2"));
        addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);

        parentNode = (DefaultMutableTreeNode) defaultTreeModel.getRoot();
        node = new DefaultMutableTreeNode(new treeobject("3", "node3"));
        addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);

        parentNode = node;
        node = new DefaultMutableTreeNode(new treeobject("4", "node4"));
        addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);
        
        

        return jTree;
    }

    private static void addNodeToDefaultTreeModel(DefaultTreeModel treeModel, DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node) {

        treeModel.insertNodeInto(node, parentNode, parentNode.getChildCount());

        if (parentNode == treeModel.getRoot()) {
            treeModel.nodeStructureChanged((TreeNode) treeModel.getRoot());
        }
    }

    
    class treeobject {

        private String id;
        private String name;

        public treeobject(String id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return getName();
        }

        /**
         * @return the id
         */
        public String getId() {
            return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(String id) {
            this.id = id;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane2.setViewportView(jTree1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(223, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(tree.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(tree.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(tree.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(tree.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new tree().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
